package cn.itsource.ymcc.mq;

import cn.itsource.ymcc.constant.NoticeConstant;
import cn.itsource.ymcc.domain.MessageStation;
import cn.itsource.ymcc.dto.CourseNoticeDto;
import cn.itsource.ymcc.service.IMessageStationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@RocketMQMessageListener(topic = NoticeConstant.TOPIC,
        consumerGroup = "courseNotice-station",
        selectorExpression = "*",
        messageModel = MessageModel.BROADCASTING
)
@Slf4j
public class StationComsumer implements RocketMQListener<CourseNoticeDto> {

    @Autowired
    private IMessageStationService stationService;

    @Override
    public void onMessage(CourseNoticeDto courseNoticeDto) {
        MessageStation station = new MessageStation();
        station.setTitle("课程发布");
        String content = String.format(NoticeConstant.COURSE_ONLINE_CONET,courseNoticeDto.getPath(),courseNoticeDto.getTitle() );
        station.setContent(content);
        station.setType("系统消息");
        station.setSendTime(new Date());
        station.setIsread(0);
        // 查询用户列表，获取用户id;
        station.setUserId(1L);
        stationService.insert(station);
        log.info("发站内信通知成功{}",station);
    }
}
